//
//  RPPickerView.m
//  ShowYork
//
//  Created by hxh on 2020/8/26.
//  Copyright © 2020 hxh. All rights reserved.
//

#import "RPPickerView.h"
typedef void(^doneBlock)(NSString *content);
@interface RPPickerView ()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (nonatomic,strong)UIButton *cancelBtn;
@property (nonatomic,strong)UIButton *confirmBtn;
@property (nonatomic,strong)UIView *containView;
@property (nonatomic,strong)UIView *toolView;
@property (nonatomic,strong)UIPickerView *pickView;

@property (nonatomic,copy)NSArray *dataArray;

@property (nonatomic,copy)doneBlock done;

@property (nonatomic,copy)NSString *currentContent;
@end

@implementation RPPickerView

- (instancetype)initWithDataArray:(NSArray *)dataArray andCompleteBlock:(void (^)(NSString * content))completeBlock{
    if (self = [super init]) {
        self.dataArray = dataArray;
        [self setUI];
        if (completeBlock) {
            __weak typeof(self)weakSelf = self;
            self.done = ^(NSString *content) {
                __strong typeof(weakSelf)strongSelf = weakSelf;
                completeBlock(strongSelf.currentContent);
            };
        }
    }
    return self;
}

- (void)setUI{
    self.frame = CGRectMake(0, 0, kScreenW, kScreenH);
    self.backgroundColor = RGBA(0, 0, 0, 0.3);
    
    
    self.containView = [[UIView alloc]init];
    self.containView.backgroundColor = [UIColor clearColor];
    [self addSubview:self.containView];
    
    
    
    [self.containView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self);
        make.height.mas_equalTo(230 + GK_SAVEAREA_BTM);
    }];
    
    self.toolView = [[UIView alloc]init];
    self.toolView.backgroundColor = RGBA(242, 242, 242, 1);
    [self.containView addSubview:self.toolView];
    [self.toolView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.containView);
        make.height.mas_equalTo(44);
    }];
    self.cancelBtn = [[UIButton alloc]init];
    [self.cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [self.cancelBtn setTitleColor:[RPAppManager themColor] forState:UIControlStateNormal];
    self.cancelBtn.titleLabel.font = font(17);
    [self.cancelBtn addTarget:self action:@selector(cancelMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.toolView addSubview:self.cancelBtn];
    [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.equalTo(self.toolView);
        make.width.mas_equalTo(100);
    }];
    
    self.confirmBtn = [[UIButton alloc]init];
    [self.confirmBtn setTitle:@"确定" forState:UIControlStateNormal];
    [self.confirmBtn setTitleColor:[RPAppManager themColor] forState:UIControlStateNormal];
    self.confirmBtn.titleLabel.font = font(17);
    [self.confirmBtn addTarget:self action:@selector(confirmMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.toolView addSubview:self.confirmBtn];
    [self.confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.right.equalTo(self.toolView);
        make.width.mas_equalTo(100);
    }];
    
    self.pickView = [[UIPickerView alloc]init];
    self.pickView.backgroundColor = [UIColor whiteColor];
    self.pickView.delegate = self;
    self.pickView.dataSource = self;
    [self.containView addSubview:self.pickView];
    
    [self.pickView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.containView);
        make.top.equalTo(self.toolView.mas_bottom);
    }];
 
    // 强制转frame,用于画圆角
    [self.toolView layoutIfNeeded];
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.toolView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(15, 15)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.toolView.bounds;
    maskLayer.path = maskPath.CGPath;
    self.toolView.layer.mask = maskLayer;
    
}

#pragma mark - action
- (void)cancelMethod{
    [self removeFromSuperview];
}
- (void)confirmMethod{
    
    if (self.currentContent.length == 0) {
        self.currentContent = [self.dataArray firstObject];
    }
    
    if (self.done) {
        self.done(self.currentContent);
    }
    [self removeFromSuperview];
}

#pragma mark - pickViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return self.dataArray.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger) row forComponent:(NSInteger)component {
    return self.dataArray[row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    self.currentContent = self.dataArray[row];
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 44.0f;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        [pickerLabel setTextAlignment:NSTextAlignmentCenter];
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        [pickerLabel setFont:[UIFont boldSystemFontOfSize:22]];
    }
    
    // Fill the label text here
    pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
    
    return pickerLabel;
}

- (void)show{
    [[UIApplication sharedApplication].keyWindow addSubview:self];    
}

- (void)drawRect:(CGRect)rect{
    [super drawRect:rect];
    UIView *v = self.pickView.subviews[1];
    v.backgroundColor = [UIColor clearColor];
    v.cl_x = 0;
    v.width = kScreenW;
    
    UILabel *topLine = [[UILabel alloc]init];
    topLine.backgroundColor = RGBA(230, 230, 230, 1);
    topLine.frame = CGRectMake(0,0, kScreenW, 1);
    
    UILabel *bottomLine = [[UILabel alloc]init];
    bottomLine.backgroundColor = RGBA(230, 230, 230, 1);
    bottomLine.frame = CGRectMake(0,v.cl_height, kScreenW, 1);

    [v addSubview:topLine];
    [v addSubview:bottomLine];
    
    
    
}

@end
